/**
 * 初始化轮滑进销存储详情对话框
 */
var SkateInfoDlg = {
    skateInfoData : {},
    validateFields: {
    	name: {
            validators: {
                notEmpty: {
                    message: '名称不能为空'
                }
            }
        }
    }
    
};


/**
 * 验证数据是否为空
 */
SkateInfoDlg.validate = function () {
    //$('#skateInfoForm').data("bootstrapValidator").resetForm();
    $('#skateInfoForm').bootstrapValidator('validate');
    return $("#skateInfoForm").data('bootstrapValidator').isValid();
};

/**
 * 清除数据
 */
SkateInfoDlg.clearData = function() {
    this.skateInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
SkateInfoDlg.set = function(key, val) {
    this.skateInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
SkateInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
SkateInfoDlg.close = function() {
    parent.layer.close(window.parent.Skate.layerIndex);
}

/**
 * 收集数据
 */
SkateInfoDlg.collectData = function() {
    this
    .set('id')
    .set('name')
    .set('type')
    .set('level')
    .set('color')
    .set('number')
    .set('price')
    .set("image")
    ;
    //console.log(SkateInfoDlg.skateInfoData.number);
}

/**
 * 提交添加
 */
SkateInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();
    
    if (!SkateInfoDlg.validate()) {
        return;
    }
    
    //附件
    var imgObject = {};
	var i = 0;
	$("#demoList tr").each(function(){
		var url = $(this).find("#fileUrl").val();    		
		var name = $(this).find("#fileName").val();
		if (typeof(url) != "undefined" && url != "") {
			if (typeof(name) == "undefined" && name == "") {
				i = i + 1;
				name = "f附件"+i;
			}
			imgObject[name]=url;
		}
	});
	var imgJsonStr="";
	if (!$.isEmptyObject(imgObject)) {
		imgJsonStr = JSON.stringify(imgObject);
	}
	console.log(imgJsonStr);

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/skate/add", function(data){
        Feng.success("添加成功!");
        window.parent.Skate.table.refresh();
        SkateInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    this.skateInfoData.sum =  this.skateInfoData.price * this.skateInfoData.number;
    this.skateInfoData.fileUrl = imgJsonStr;
    console.log(this.skateInfoData);
    ajax.set(this.skateInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
SkateInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();
    
    if (!SkateInfoDlg.validate()) {
        return;
    }
    
  //附件
    var imgObject = {};
	var i = 0;
    $("#demoList tr").each(function(){
		var url = $(this).find("#fileUrl").val();    		
		var name = $(this).find("#fileName").val();
		if (typeof(url) != "undefined" && url != "") {
			if (typeof(name) == "undefined" && name == "") {
				i = i + 1;
				name = "附件"+i;
			}
			imgObject[name]=url;
		}
	});
	var imgJsonStr="";
	if (!$.isEmptyObject(imgObject)) {
		imgJsonStr = JSON.stringify(imgObject);
	}
	
	console.log(imgJsonStr);
	
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/skate/update", function(data){
        Feng.success("修改成功!");
        window.parent.Skate.table.refresh();
        SkateInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    this.skateInfoData.sum =  this.skateInfoData.price * this.skateInfoData.number;
    this.skateInfoData.fileUrl = imgJsonStr;
    ajax.set(this.skateInfoData);
    ajax.start();
}



$(function() {
	
	 Feng.initValidator("skateInfoForm", SkateInfoDlg.validateFields);
	
	if($("#id").val()!=null && $("#id").val()!="" && $("#id").val()!=undefined){
		if($(".file_date").length==0){
			$("#no_file").show();
		}
	}
	
    var uploader = WebUploader.create({
    	auto: true, // 选完文件后，是否自动上传
    	pick: { 
    		id: "#uploadImage", 
    		innerHTML: "上传文件",
    		multiple : true,
    	},
    	 accept: {
             title: 'Images',
             extensions: 'gif,jpg,jpeg,bmp,png',
             //mimeTypes: 'image/*'
             mimeTypes : 'image/gif,image/jpg,image/jpeg,image/bmp,image/png'
         },
         swf : Feng.ctxPath + '/static/css/plugins/webuploader/Uploader.swf',
         disableGlobalDnd : true,
         //线程数
         threads: 2,
         duplicate: true, //是否允许重复的图片
         server: Feng.ctxPath + '/filemeta/uploadFile', // 文件接收服务端
         fileSingleSizeLimit: 100 * 1024 * 1024, //单个文件大小限制
         fileNumLimit:10, //上传文件数量限制
         compress:false,//上传前不压缩
    });
    
    uploader.on('uploadSuccess', function(file,response) {
    	console.log(file);
    	console.log(response);
    	
    	var type= response.fileName.substring(response.fileName.lastIndexOf(".")+1);
    	var tmp = '<tr id="upload-'+file.id+'" data="'+response.fileUrl+'">';
    	tmp += '<td class ="file_date" style="text-align:center; border-width: 1px;padding: 8px; border-style: solid; border-color: #e6e6e6;">'+ response.fileName;
    	tmp += '</td>';
    	tmp += '<td style="text-align:center; border-width: 1px;padding: 8px; border-style: solid; border-color: #e6e6e6;">'+type;
    	tmp += '</td>';
    	tmp += '<td style="text-align:center; border-width: 1px;padding: 8px; border-style: solid; border-color: #e6e6e6;">等待提交</td>';
    	tmp += '<td style="text-align:center; border-width: 1px;padding: 8px; border-style: solid; border-color: #e6e6e6;"><input type="hidden" id="fileName" value=""/><input type="hidden" id="fileUrl" value=""/>';
    	tmp += '<a class="file_delete" style="width:40px;height:20px;display:inline-block;background:#ff5722;line-height:20px;color:#ffffff;text-align:center;">删除</a>';
    	tmp += '</td></tr>';
    	$('#demoList').append(tmp);
    	$("#upload-"+file.id).find("#fileName").val(response.fileName);
		$("#upload-"+file.id).find("#fileUrl").val(response.fileUrl);
    	if($(".file_date").length>0){
			$("#no_file").remove();
		}
		/*
		if(type == ".pdf"){
			tmp += "<a data='"+res.fileUrl+"' class='layui-btn layui-btn-mini file_btn file_view'>浏览</a>";
		}
		*/
	});
});

//删除上传文件
$(document).on("click", ".file_delete", function(){
	$(this).parent().parent().remove();
	if($(".file_date").length==0){
		$("#no_file").show();
	}
})

$(document).on("click",".file_view",function(){
	//查看附件
		var fileUrl = $(this).attr("data");
		console.log(fileUrl);
		var index = layer.open({
			type: 2,
			area:["100%","100%"],
			//content:'https://www.baidu.com/',
			//content:Feng.ctxPath + "/static/temporaryFolder/20210122192624967.jpg",
			content: Feng.ctxPath + fileUrl,
			maxmin:true
		});
		this.layerIndex = index;
});

//强制下载文件
$(document).on("click",".file_download",function(){
	var fileName = $(this).attr("data-name");
	var fileUrl =$(this).attr("data-url");
	window.location.href="/base/downFile?fileUrl="+fileUrl+"&fileName="+fileName.substr(0,fileName.lastIndexOf("."));
})





